#include "GuiInventory.hpp"
#include "../Entity/EntityPlayer.hpp"
#include "../InventorySystem/InventoryManager.hpp"
#include "../Item/ItemStack.hpp"
#include "../Item/Item.hpp"
#include "../InventorySystem/ContainerPlayer.hpp"
#include "GuiSlots.hpp"
#include "GuiIngame.hpp"
#include "../MapSystem/MapView.hpp"

GuiInventory::GuiInventory(Core* core)
{
    m_core = core;
    m_player = core->thePlayer;
    m_inventory = m_player->getInventory()->getInventory();

    if(!m_backgroundTexture.loadFromFile("Ressources/Sprites/guiInventory.png"))
    {

    }
    m_background.setTexture(m_backgroundTexture);
    m_background.setTextureRect(sf::IntRect(0,0,344,317));
    m_background.setPosition(m_core->theWorld->getCamera().getSize().x/2-172, m_core->theWorld->getCamera().getSize().y/2 -158);
    initGui();
}

GuiInventory::~GuiInventory()
{

}

void GuiInventory::initGui()
{
    for(int x = 0; x< m_inventory.size();x++)
    {
        buttonList.push_back(new GuiSlots(x, m_core->theWorld->getCamera().getSize().x/2-172+8, m_core->theWorld->getCamera().getSize().y/2-158+25 + 36*x, m_inventory[x]));
    }
    GuiScreen::initGui();
}

void GuiInventory::drawScreen()
{
    m_core->window.setView(*m_core->guiView);
    m_core->window.draw(m_background);
    if (selectedButton != NULL)
    {
        GuiSlots* guiSlot = dynamic_cast<GuiSlots*> (selectedButton);
        Item* item = guiSlot->getItemStack()->getItem();
        sf::Sprite sp = *item->getSprite();
        sp.setPosition(m_core->theWorld->getCamera().getSize().x/2- 8,m_core->theWorld->getCamera().getSize().y/2 + 60);
        m_core->window.draw(sp);
        m_core->window.setView(m_core->window.getDefaultView());
        m_core->fontRenderer->printCenteredString(item->getName(),(WINDOW_WIDTH/VIEW_WIDTH)*(VIEW_WIDTH/2+ 30),(WINDOW_HEIGHT/VIEW_HEIGHT)*(VIEW_HEIGHT/2 + 50), (WINDOW_WIDTH/VIEW_WIDTH)*134, (WINDOW_HEIGHT/VIEW_HEIGHT)*50, sf::Color(0,0,0), 24);
        m_core->fontRenderer->printCenteredString(item->getDescription(),(WINDOW_WIDTH/VIEW_WIDTH)*(VIEW_WIDTH/2 - 15),(WINDOW_HEIGHT/VIEW_HEIGHT)*(VIEW_HEIGHT/2 + 100), (WINDOW_WIDTH/VIEW_WIDTH)*184,(WINDOW_HEIGHT/VIEW_HEIGHT)*54, sf::Color(0,0,0), 15);
    }
    m_core->window.setView(*m_core->guiView);
    GuiScreen::drawScreen();
}

void GuiInventory::onGuiClosed()
{

}

void GuiInventory::actionPerformed(GuiButton* button)
{
    GuiSlots* guiSlot = dynamic_cast<GuiSlots*> (button);
    //guiSlot->interract();
}

bool GuiInventory::doesGuiPauseGame()
{
    return true;
}
